﻿using System;
using FlowCanvas;
using FlowCanvas.Nodes;
using LogSystem;
using NodeCanvas.Framework;
using ParadoxNotion;
using ParadoxNotion.Design;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    [Category("Ggame")]
    public class RpgLevelBattleEvent : EventNode
    {
        public static Action<int, bool> OnBattleEvent;

        [RequiredField]
        public BBParameter<int> levelId;

        private FlowOutput onSuccess;
        private FlowOutput onFailed;
        
        
        public override string name
        {
            get
            {
                return string.Format("{0} ({1})", "RpgLevelBattleEvent", levelId.value);
            }
        }
        
        protected override void RegisterPorts()
        {
            AddFlowInput("start",OnStart);

            onSuccess = AddFlowOutput("OnSuccess");
            onFailed = AddFlowOutput("OnFail");
        }

        public void OnStart(Flow f)
        {
            OnBattleEvent -= OnRpgBattleEvent;
            OnBattleEvent += OnRpgBattleEvent;
        }

        private void OnRpgBattleEvent(int level, bool success)
        {
            if (level == levelId.value)
            {
                if (success)
                {
                    onSuccess.Call(new Flow());
                }
                else
                {
                    onFailed.Call(new Flow());
                }
            }
        }

        public override void OnPostGraphStoped()
        {
            OnBattleEvent -= OnRpgBattleEvent;
        }
    }
}